<template>
  <div class="app-container">
    <div
      class="header"
      style="display: flex; justify-content: space-between"
    >
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item>
          <el-button
            icon="refresh"
            color=" rgb(69, 76, 105)"
            style="color: #fff"
            @click="resetQuery()"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleExport"
            color="rgb(146,226,206)"
            style="color: rgb(120, 120, 120); padding: 0 20px"
            v-hasPermi="['system:set:import']"
          >
            导 出
          </el-button>
        </el-form-item>
      </el-form>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item
          label=""
          prop="state"
        >
          <el-select style="width: 190px"
            v-model="queryParams.applicationType"
            placeholder="请选择申请类型"
            clearable
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="(item, index) in allAppType"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="state">
          <el-select style="width: 190px" v-model="queryParams.state" placeholder="请选择状态" clearable @keyup.enter="handleQuery">
            <el-option
              v-for="(item, index) in allState"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入联系电话"
          ></el-input>
        </el-form-item>


<!--        <el-form-item-->
<!--          label=""-->
<!--          prop="applicationTime"-->
<!--        >-->
<!--          <el-date-picker-->
<!--            clearable-->
<!--            v-model="queryParams.applicationTime"-->
<!--            type="daterange"-->
<!--            value-format="YYYY-MM-DD"-->
<!--            start-placeholder="申请开始时间"-->
<!--            end-placeholder="结束时间"-->
<!--          >-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->

        <el-form-item label="" style="width: 308px;">
          <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="申请时间"
              end-placeholder="结束时间"
          ></el-date-picker>
        </el-form-item>


        <el-form-item>
          <el-button
            type="info"
            icon="Search"
            @click="handleQuery()"
          />
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="loading"
      :data="applicationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="申请类型"
        align="center"
        prop="applicationType"
      >
<!--        <template #default="scope">-->
<!--          {{-->
<!--            allAppType.find((item) => (item.value = scope.row.applicationType))-->
<!--              .label-->
<!--          }}-->
<!--        </template>-->
        <template #default="scope">
          <el-tag v-if="scope.row.applicationType == 0" type="primary">服务站</el-tag>
          <el-tag v-if="scope.row.applicationType == 1" type="primary">区县分公司</el-tag>
          <el-tag v-if="scope.row.applicationType == 2" type="primary">市级分公司</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="所属区域"
        align="center"
        prop="location"
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="userName"
      />
      <el-table-column
        label="联系电话"
        align="center"
        prop="phone"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
      />
      <el-table-column
        label="申请时间"
        align="center"
        prop="applicationTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.applicationTime, "{y}-{m}-{d}  {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="审核状态"
        align="center"
        prop="state"
      >
        <template #default="scope">
          {{ allState.find((item) => (item.value = scope.row.state)).label }}
        </template>
      </el-table-column> -->

      <!-- 加入的 -->
      <el-table-column
        label="审核状态"
        align="center"
        prop="state">
        <template #default="scope">
          <el-tag v-if="scope.row.state == 0" type="primary">待审核</el-tag>
          <el-tag v-if="scope.row.state == 1" type="success">申请成功</el-tag>
          <el-tag v-if="scope.row.state == 2" type="danger">申请失败</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="处理时间"
        align="center"
        prop="dealTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.dealTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width"  fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" v-if="scope.row.state == 0" @click="handleUpdate(scope.row),isShow(true)"

                     v-hasPermi="['system:application:edit']">处理
          </el-button>
          <el-button link type="primary" icon="View" v-if="scope.row.state == 1" @click="handleUpdate(scope.row),isShow(false)"
                     v-hasPermi="['system:application:view']">查看
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:application:remove']">删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改加入平台申请对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="applicationRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="申请类型"
          prop="form.applicationType"
          ><el-select
            style="width: 190px"
            v-model="form.applicationType"
            disabled
            placeholder="请选择申请类型"
            clearable
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="item in allAppType"
              :value="item.value"
              :key="item.value"
              :label="item.label"

            ></el-option>
          </el-select>
        </el-form-item>



        <el-form-item
          label="所属区域"
          prop="location"
        >
          <el-input
            v-model="form.location"
            disabled
            placeholder="请输入所属区域"
          />
        </el-form-item>
        <el-form-item
          label="姓名"
          prop="userName"
        >
          <el-input
            v-model="form.userName"
            disabled
            placeholder="请输入姓名"
          />
        </el-form-item>
        <el-form-item
          label="电话"
          prop="phone"
        >
          <el-input
            v-model="form.phone"
            disabled
            placeholder="请输入电话"
          />
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remark"
        >
          <el-input
            v-model="form.remark"
            disabled
            placeholder="请输入备注"
          />
        </el-form-item>
        <el-form-item
          label="申请处理"
          prop="state"
        >
          <el-radio-group v-model="form.state">
<!--            <el-radio label="0">待审核</el-radio>-->
            <el-radio label="1">申请成功</el-radio>
            <el-radio label="2">申请失败</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="处理详情"
          prop="dealDetails"
        >
          <el-input
            type="textarea"
            v-model="form.dealDetails"
            :rows="4"
            placeholder="请输入处理详情"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer" >
          <el-button type="primary" @click="submitForm" v-if="flag===1">确 定</el-button>
          <el-button @click="cancel" v-if="flag===1">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PlatformApplication">
import {
  listPlatformApplication,
  getPlatformApplication,
  delPlatformApplication,
  addPlatformApplication,
  updatePlatformApplication,
} from "@/api/system/application";

const { proxy } = getCurrentInstance();
const flag = ref(0);
const applicationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  allState: [
    {
      label: "待审核",
      value: 0,
    },
    {
      label: "申请成功",
      value: 1,
    },
    {
      label: "申请失败",
      value: 2,
    },
  ],
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    applicationType: null,
    applicationTime: null,
    state: null,
    phone:null
  },
  rules: {
    applicationType: [
      { required: true, message: "申请类型不能为空", trigger: "change" },
    ],
    zoneId: [
      { required: true, message: "所属区域ID不能为空", trigger: "blur" },
    ],
    applicationManId: [
      { required: true, message: "申请人ID不能为空", trigger: "blur" },
    ],
    applicationRole: [
      { required: true, message: "申请人角色不能为空", trigger: "blur" },
    ],
    state: [{ required: true, message: "审核状态不能为空", trigger: "blur" }],
    dealDetails: [
      { required: true, message: "处理详情不能为空", trigger: "blur" },
    ],
  },
  allAppType: [
    {
      value: '0',
      label: "服务站",
    },
    {
      value: '1',
      label: "区县分公司",
    },
    {
      value: '2',
      label: "市级分公司",
    },
  ]
});

const { queryParams, form, rules, allAppType, allState } = toRefs(data);
//是否能继续修改
const isShow = (flagStatus) => {
  if(flagStatus){
    flag.value=1;
  }else{
    flag.value=0;
  }
}
/** 查询加入平台申请列表 */
function getList() {
  loading.value = true;
  listPlatformApplication(proxy.addDateRange(queryParams.value,dateRange.value)).then((response) => {
    console.log(response,'repons返回的申请数据');
    applicationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    applicationId: null,
    applicationType: null,
    zoneId: null,
    applicationManId: null,
    applicationRole: null,
    remark: null,
    applicationTime: null,
    state: null,
    dealTime: null,
    dealDetails: null,
    phone:null,
  };
  proxy.resetForm("applicationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.applicationType = null;
  queryParams.value.state = null;
  queryParams.value.userName = null;
  queryParams.value.phone = null;

  proxy.resetForm("queryRef");
  dateRange.value = [];
  getList();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.applicationId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加加入平台申请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _applicationId = row.applicationId || ids.value;
  getPlatformApplication(_applicationId).then((response) => {
    console.log(response,'response修改按钮时获取到的修改数据');
    form.value = response.data;
    open.value = true;
    title.value = "申请处理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["applicationRef"].validate((valid) => {
    if (valid) {
      console.log(form.value,'form.value修改生申请时德提交的数据有误吗')
      if (form.value.applicationId != null) {
        updatePlatformApplication(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          title.value = "申请处理";
          getList();
        });
      } else {
        addPlatformApplication(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _applicationIds = row.applicationId || ids.value;
  proxy.$modal
    .confirm(
      '是否确认删除加入平台申请编号为"' + _applicationIds + '"的数据项？'
    )
    .then(function () {
      return delPlatformApplication(_applicationIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/application/export",
    {
      ...queryParams.value,
    },
    `application_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
